//
//  ThreeViewController.m
//  MainDemo
//
//  Created by FM on 2017/1/9.
//  Copyright © 2017年 FM. All rights reserved.
//

#import "ThreeViewController.h"
#import "UIScrollView+MJRefreshAdd.h"
@interface ThreeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView*  tableView;
@property(nonatomic,strong)NSDictionary* dataDict;
@property(nonatomic,strong)NSArray*  dataArr;
@property(nonatomic,strong)NSMutableArray*   isOpenArr;
@property(nonatomic,strong)NSArray* sectionNameArr;
@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title=@"内机监控";
    self.view.backgroundColor=[UIColor whiteColor];
    NSArray*  firstDataArr=@[@"空凋01",@"空凋03",@"空调04"];
    NSArray*  secondArr=@[@"空凋01",@"空调02",@"空凋03",@"空调04"];
    NSArray*  thirdArr=@[@"空凋01",@"空调04"];
    _sectionNameArr=@[@"客厅",@"卧室",@"餐厅"];
    self.isOpenArr=[[NSMutableArray alloc] init];
    // self.dataDict=@{@"first":firstDataArr,@"second":secondArr,@"third":thirdArr};
    self.dataArr=@[firstDataArr,secondArr,thirdArr];
    //for (int i=0; i<self.dataDict.allKeys.count; i++) {
    for (int i=0; i<self.dataArr.count; i++) {
        NSString*  state=@"close";
        [self.isOpenArr addObject:state];
    }
    [self loadTableView];
}
-(void)loadTableView
{
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.sectionFooterHeight=0;//从第二个开始，每个section之间的距离
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
//    [self.tableView addRefreshWithTarget:self headerSelect:@selector(headerWithRefresh) footerSelect:@selector(footerResh)];
}
-(void)headerWithRefresh{
    
    [self.tableView stopHeaderRefresh];
}
-(void)footerResh{
    
    
}
-(void)ClickSection:(UIButton*)sender
{
    NSString*  state=[self.isOpenArr objectAtIndex:sender.tag-100];
    if ([state isEqualToString:@"open"]) {
        state=@"close";
    }else
    {
        state=@"open";
    }
    self.isOpenArr[sender.tag-100]=state;
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:sender.tag-100];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // return self.dataDict.allKeys.count;
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString*  state=[self.isOpenArr objectAtIndex:section];
    if ([state isEqualToString:@"open"]) {
        // NSString*  key=[self.dataDict.allKeys objectAtIndex:section];
        //   NSArray*  arr=[self.dataDict objectForKey:key];
        NSArray*  arr=[self.dataArr objectAtIndex:section];
        return arr.count;
    }
    return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString* identifer=@"cell";
    UITableViewCell*  cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell) {
        cell=[[UITableViewCell alloc] init];
    }
    //   NSString*  key=[[self.dataDict allKeys] objectAtIndex:indexPath.section];
    //   NSArray*   arr=[self.dataDict objectForKey:key];
    NSArray*  sectionArr=[_dataArr objectAtIndex:indexPath.section];
    //   cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text=[sectionArr objectAtIndex:indexPath.row];
    UILabel*  stateLabel=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH-100, 0, 80, 50)];
    stateLabel.text=@"设备正常";
    [cell.contentView addSubview:stateLabel];
    UIImageView*  stateImageView=[[UIImageView alloc] initWithFrame:CGRectMake(WIDTH-130, 13, 25, 25)];
    stateImageView.backgroundColor=[UIColor redColor];
    [cell.contentView addSubview:stateImageView];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView*  sectionBackView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    sectionBackView.backgroundColor=[UIColor whiteColor];
    UIImageView*  lineImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 49, WIDTH, 1)];
    [lineImageView setImage:[UIImage imageNamed:@"Google模糊灰.png"]];
    [sectionBackView addSubview:lineImageView];
    UILabel*  nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(20, 0, WIDTH/2, 50)];
    nameLabel.text=[_sectionNameArr objectAtIndex:section];
    [sectionBackView addSubview:nameLabel];
    UIButton*  button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, WIDTH, 50)];
    button.tag=100+section;
    [button addTarget:self action:@selector(ClickSection:) forControlEvents:UIControlEventTouchUpInside];
    [sectionBackView addSubview:button];
    UILabel*  numLabel=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH-70, 0, 20, 50)];
    NSArray* subArr=[_dataArr objectAtIndex:section];
    numLabel.text=[NSString stringWithFormat:@"%zd",subArr.count];
    [sectionBackView addSubview:numLabel];
    UIImageView* stateImage=[[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-30, 20, 20, 10)];
    if ([[_isOpenArr objectAtIndex:section] isEqualToString:@"open"]) {
        [stateImage setImage:[UIImage imageNamed:@"sectionOpen"]];
    }
    else if ([[_isOpenArr objectAtIndex:section] isEqualToString:@"close"]) {
        [stateImage setImage:[UIImage imageNamed:@"sectionClose"]];
    }
    [sectionBackView addSubview:stateImage];
    return sectionBackView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


